package com.marktotop.controller;

import com.marktotop.pojo.Payment;
import com.marktotop.response.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * OrderController
 *
 * @author WXLD
 * @Data 2021/7/3
 * @Since 1.0
 */
@RestController
@Slf4j
@RequestMapping("/consumer/order")
@Api(tags = "支付订单号消费接口")
public class OrderController {

    // 单机版
//    private static final String PAYMENT_URL = "http://localhost:8001";
    /**
     * 集群版
     *
     * 不关心地址，只关心需要的微服务名称
     */
    private static final String PAYMENT_URL = "http://zookeeper-payment-service";

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/create")
    @ApiOperation(value = "创建流水号")
    public ResultBean create(Payment payment){
        return restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment,ResultBean.class);
    }

    @GetMapping("/get/{id}")
    @ApiOperation(value = "根据id查询流水号")
    public ResultBean getPayment(@PathVariable("id") Long id){
        return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,ResultBean.class);
    }

    @GetMapping("/zk")
    @ApiOperation(value = "访问zookeeper")
    public String paymentZk(){
        return restTemplate.getForObject(PAYMENT_URL+"/payment/zk",String.class);
    }
}
